//
//  HYPhotoImportViewController.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/8.
//  相册导入

#import "HYPhotoImportViewController.h"
#import "HYPhotoVideoViewModel.h"
#import "HYPhotoSelectViewCell.h"
#import "HYVideoRoomViewController.h"
#import "HYPhotoBottomView.h"

@interface HYPhotoImportViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) HYPhotoVideoViewModel *viewModel;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) HYPhotoBottomView *bottomView;

@property (nonatomic, strong) UIColor *oKButtonTitleColorNormal;

@property (nonatomic, strong) UIColor *oKButtonTitleColorDisabled;

@end

@implementation HYPhotoImportViewController

- (void)dealloc
{
    CLog(@"dealloc -- %@",[self class]);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = NSLocalizedString(@"相册导入", nil);
    self.viewModel.folderIdentification = self.folderIdentification;
    self.oKButtonTitleColorNormal   = [UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:1.0];
    self.oKButtonTitleColorDisabled = [UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:0.5];
    self.navigationBarView.navagationBarStyle = Left_right_button_show;
    [self.navigationBarView.rightButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [self.navigationBarView.rightButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateDisabled];
    [self.navigationBarView.rightButton setTitleColor:self.oKButtonTitleColorNormal forState:UIControlStateNormal];
    [self.navigationBarView.rightButton setTitleColor:self.oKButtonTitleColorNormal forState:UIControlStateHighlighted];
    [self.navigationBarView.rightButton setTitleColor:self.oKButtonTitleColorDisabled forState:UIControlStateDisabled];
    [self configUI];
    [self bindSignal];
    [self getData];
}

- (void)leftButtonClick
{
    [self.navigationBarView.rightButton setTitleColor:AppNavTitleThemeColor forState:UIControlStateNormal];
    [self.navigationBarView.rightButton setTitleColor:AppNavTitleThemeColor forState:UIControlStateHighlighted];
    [self.navigationBarView.rightButton setTitleColor:[UIColor systemGrayColor] forState:UIControlStateDisabled];
    [super leftButtonClick];
}

- (void)rightButtonClick
{
    [self.viewModel cancelSelectPhotoVideo];
}

- (void)configUI
{
    self.viewModel.maxSelectCount = self.maxSelectCount;
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(44+HOME_BAR_HEIGHT);
    }];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAVIGATIONBAR_HEIGHT);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        if([x count]){
            self.collectionView.hidden = NO;
            [self.collectionView reloadData];
        }else{
            [HYHub showMessage:NSLocalizedString(@"本地相册暂无视频", nil)];
        }
    }];
    [self.viewModel.saveSuccessSubject subscribeNext:^(id x) {
        @strongify(self);
        [self leftButtonClick];
    }];
    // 同步完成
    [self.viewModel.syncSubject subscribeNext:^(id x) {
        [HYHub dismiss];
    }];
    
    RAC(self.bottomView, selectCount) = [RACObserve(self.viewModel, selectCount) skip:1];
    RAC(self.navigationBarView.rightButton,enabled) = RACObserve(self.bottomView.finishBtn, enabled);
}

- (void)getData
{
    [HYHub loadMessage:NSLocalizedString(@"icloud视频同步中...", nil)];
    /// 获取相册权限
    [self.viewModel fetchPhotoVideoAuthAndSources];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsInSection];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYPhotoSelectViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYPhotoSelectViewCell" forIndexPath:indexPath];
    [cell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.viewModel isCanPlay:indexPath]){
        [self playLocalPhotoVideo:[self.viewModel didSelectRowAtIndexPath:indexPath]];
    }else{
        if([HYNetWorkManager shareManager].currentReachabilityStatus == NotReachable){
            [HYHub showMessage:NSLocalizedString(@"当前是icloud视频,请连接网络", nil)];
        }else{
            [HYHub showMessage:NSLocalizedString(@"icloud视频同步中...", nil)];
        }
    }
}

- (void)playLocalPhotoVideo:(HYVideoViewModel *)viewModel
{
    viewModel.videoConfig.isCancelPlayHistory = YES;
    HYVideoRoomViewController *basePlayerVC = [[HYVideoRoomViewController alloc]initWithViewModel:viewModel];
    [self.navigationController pushViewController:basePlayerVC animated:YES];
}

#pragma mark - loadLazy

- (HYPhotoBottomView *)bottomView
{
    if(!_bottomView){
        @weakify(self);
        _bottomView = [[HYPhotoBottomView alloc]initWithFinishBlock:^{
            @strongify(self);
            [self.viewModel saveSelectPhotoVideo];
        }];
        _bottomView.oKButtonTitleColorNormal = self.oKButtonTitleColorNormal;
        _bottomView.oKButtonTitleColorDisabled = self.oKButtonTitleColorDisabled;
    }
    return _bottomView;
}

- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kItemWidth(3), kItemHeight(3));
        layout.minimumLineSpacing = kItemSpace;
        layout.minimumInteritemSpacing = kItemSpace;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = AppBgThemeColor;
        _collectionView.contentInset = UIEdgeInsetsMake(kItemSpace, kItemSpace, kItemSpace, kItemSpace);
        [_collectionView registerClass:[HYPhotoSelectViewCell class] forCellWithReuseIdentifier:@"HYPhotoSelectViewCell"];
    }
    return _collectionView;
}

- (HYPhotoVideoViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[HYPhotoVideoViewModel alloc]init];
        _viewModel.isSuperPrivate = self.isPrivate;
    }
    return _viewModel;
}

@end
